package com.module.oa.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.module.oa.domain.MzActivitiMainInfo;
import com.module.oa.service.IMzActivitiMainInfoService;
import com.module.oa.util.Util;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.module.oa.domain.MzActivitiMain;
import com.module.oa.service.IMzActivitiMainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author hezhengxian
 * @date 2023-07-26
 */
@RestController
@RequestMapping("/activiti_main")
public class MzActivitiMainController extends BaseController
{
    @Autowired
    private IMzActivitiMainService mzActivitiMainService;
    @Autowired
    private IMzActivitiMainInfoService mzActivitiMainInfoService;
    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("oa:activiti_main:list")
    @GetMapping("/list")
    public TableDataInfo list(MzActivitiMain mzActivitiMain)
    {
        startPage();
        List<MzActivitiMain> list = mzActivitiMainService.selectMzActivitiMainList(mzActivitiMain);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("oa:activiti_main:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MzActivitiMain mzActivitiMain)
    {
        List<MzActivitiMain> list = mzActivitiMainService.selectMzActivitiMainList(mzActivitiMain);
        ExcelUtil<MzActivitiMain> util = new ExcelUtil<MzActivitiMain>(MzActivitiMain.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("oa:activiti_main:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(mzActivitiMainService.selectMzActivitiMainById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("oa:activiti_main:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MzActivitiMain mzActivitiMain)
    {
        mzActivitiMain.setId(Util.get_radom_id());
        mzActivitiMain.setCrteTime(new Date());
        // 自动生成流程编号
        mzActivitiMain.setActivitiId(Util.get_business_no(Util.ToPinyin(mzActivitiMain.getActivitiName())));
        // 获取当前登录用户id
        mzActivitiMain.setCrteUserId(SecurityUtils.getLoginUser().getUserid().toString());
        // 获取当前登录用户姓名
        mzActivitiMain.setCrteUserName(SecurityUtils.getLoginUser().getUsername());
        return toAjax(mzActivitiMainService.insertMzActivitiMain(mzActivitiMain));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("oa:activiti_main:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MzActivitiMain mzActivitiMain)
    {
        return toAjax(mzActivitiMainService.updateMzActivitiMain(mzActivitiMain));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("oa:activiti_main:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(mzActivitiMainService.deleteMzActivitiMainByIds(ids));
    }

    /**
     * 查看流程图
     * @param activitiId
     * @return
     */
    @RequiresPermissions("oa:attendance:list")
    @GetMapping(value = "/viewActiviti/{activitiId}")
    public AjaxResult viewActiviti(@PathVariable("activitiId") String activitiId)
    {
        List<MzActivitiMainInfo> list = mzActivitiMainInfoService.selectMzActivitiMainInfoByActivitiId(activitiId);
        AjaxResult ajax = AjaxResult.success();
        // X轴坐标不变，Y轴坐标递增250
        int X = 550;
        int Y = 100;
        String data_result = "[{name: '开始',x: "+X+",y: "+Y+"},";
        // 连线
        String link_result = "[{source: '开始',target: '"+list.get(0).getNodeName()+"'},";
        if(list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                Y+=200;
                data_result += "{name: '"+list.get(i).getNodeName()+"',x: "+X+",y: "+(Y)+"},";
                if((i+1)<list.size()){
                    link_result += "{source: '"+list.get(i).getNodeName()+"',target: '"+list.get(i+1).getNodeName()+"'},";
                }
            }
            data_result += "{name: '结束',x: "+X+",y: "+(Y+200)+"}]";
            link_result += "{source: '"+list.get(list.size()-1).getNodeName()+"',target: '结束'}]";
        }
        ajax.put("data_result",data_result);
        ajax.put("link_result",link_result);
        ajax.put("type",list.get(0).getActivitiName());
        return ajax;
    }
}
